import os, time
from flask import request, url_for


class Upload:
    def __init__(self):
        root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        self.upload_path = os.path.join(root_path, 'static', 'upload')
        root_url = request.url_root
        self.upload_url = ('/').join([root_url + 'static', 'upload'])

    def upload_one(self, file):
        filename = time.strftime('%Y%m%d%H%M%S') + file.filename
        path = os.path.join(self.upload_path, filename)
        file.save(path)
        return filename

    def path_to_url(self, path):
        return path.replace(self.upload_path, self.upload_url).replace('\\', '/')
